home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / game / board / Chaos_src.lha / chaos / src / OutAmi.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  11KB  |  457 lines

  1. /*  Chaos:                  The Chess HAppening Organisation System     V5.3
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: OutAmi.c,v $
  20.     $Revision: 3.2 $
  21.     $Date: 1994/11/19 19:32:01 $
  22.  
  23.     These are the system dependent output functions.
  24.  
  25.     Computer:   Amiga 1200                  Compiler:   Dice 2.07.54 (3.0)
  26.  
  27.     Author:     Jochen Wiedmann
  28.         Am Eisteich 9
  29.       72555 Metzingen
  30.         Tel. 07123 / 14881
  31.         Internet: jochen.wiedmann@zdv.uni-tuebingen.de
  32. */
  33.  
  34.  
  35. #ifndef CHAOS_H
  36. #include "chaos.h"
  37. #endif
  38.  
  39. #ifdef AMIGA
  40. #include <exec/libraries.h>
  41. #include <exec/execbase.h>
  42. #include <prefs/prefhdr.h>
  43. #include <prefs/printertxt.h>
  44. #include <proto/iffparse.h>
  45. #endif  /*  AMIGA   */
  46.  
  47.  
  48.  
  49.  
  50. /*
  51.     The following function makes text being centered on the printer.
  52.     This isn't needed at all and might be a stub function on other systems
  53.     than the Amiga.
  54.  
  55.     Inputs: device - the device to which the output goes
  56.  
  57.     Results: TRUE, if successfull, FALSE otherwise
  58. */
  59. int CenterText(int device)
  60.  
  61. #ifdef AMIGA    /*  The following lines produce centered output */
  62. {
  63.   if ((device == DEVICE_PrinterLQ  ||  device == DEVICE_PrinterDraft)  &&
  64.       !lprint("\2331 F"))
  65.   { return(FALSE);
  66.   }
  67.   return(TRUE);
  68. }
  69. #endif  /*  AMIGA   */
  70.  
  71.  
  72.  
  73.  
  74. /*
  75.     The InitOutput() function gets called by output functions before they
  76.     do any output via lprint() or longlprint().
  77.  
  78.     Inputs: title     - the printed document's title
  79.         prthead   - the head to print above each page (printer output)
  80.         scrhead   - the head to appear on top of the output window
  81.             (screen output)
  82.         filename  - the destination file (used if output to file only);
  83.             this can be NULL, in which case InitOutput() brings
  84.             up a filerequester asking for the filename
  85.         device    - the output device: DEVICE_Screen, DEVICE_PrinterDraft,
  86.             DEVICE_PrinterLQ, Device_FileAscii or DEVICE_FileTeX
  87.         prtlines  - the number of lines, that one item needs on the
  88.             printer
  89.         scrlines  - the number of lines, that one item needs on the
  90.             screen
  91.  
  92.     Result: TRUE, if successfull, FALSE otherwise
  93. */
  94. static char *titleOutput;
  95. static char *prtheadOutput;
  96. static char *scrheadOutput;
  97. static char *filenameOutput;
  98. static int deviceOutput;
  99. static int prtlinesOutput;
  100. static int scrlinesOutput;
  101. void *OutputMemList;
  102. struct MinList OutputList;
  103. char *OutputLine;
  104. int OutputLineLen;
  105. int OutputLineMaxLen;
  106. int OutputReturnCode;
  107.  
  108. int InitOutput(char *title, char *prthead, char *scrhead, char *filename,
  109.            char *ending, int device, int prtlines, int scrlines)
  110.  
  111. #ifdef AMIGA
  112. {
  113.   prtlinesOutput = prtlines;
  114.   scrlinesOutput = scrlines;
  115.  
  116.   OutputReturnCode = 0;
  117.   OutputMemList = NULL;
  118.   OutputLine = NULL;
  119.   OutputLineLen = OutputLineMaxLen = (device == DEVICE_Screen) ? 0 : -1;
  120.   deviceOutput = device;
  121.   NewList((struct List *) &OutputList);
  122.  
  123.   if ((device == DEVICE_FileAscii  ||  device == DEVICE_FileTeX)  &&
  124.       filename == NULL)
  125.   { if ((filename = FileRequest(NULL,
  126.                 (char *) WND_ASCIIFILE_TITLE,
  127.                 (device == DEVICE_FileTeX) ? "#?.tex" : ending,
  128.                 TRUE))
  129.           ==  NULL)
  130.     { OutputReturnCode = RETURN_WARN;
  131.       return(FALSE);
  132.     }
  133.   }
  134.  
  135.   if (!(titleOutput = GetStringMem(&OutputMemList, title))  ||
  136.       !(prtheadOutput = GetStringMem(&OutputMemList, prthead))  ||
  137.       !(scrheadOutput = GetStringMem(&OutputMemList, scrhead))  ||
  138.        (filename != NULL  &&
  139.     !(filenameOutput = GetStringMem(&OutputMemList, filename))))
  140.   { TerminateOutput();
  141.     OutputReturnCode = RETURN_ERROR;
  142.     return(FALSE);
  143.   }
  144.   return(TRUE);
  145. }
  146. #endif  /*  AMIGA   */
  147.  
  148.  
  149.  
  150.  
  151. /*
  152.     TerminateOutput() gets called from the output functions if an error
  153.     occurs or if the output is ended.
  154. */
  155. void TerminateOutput(void)
  156.  
  157. #ifdef AMIGA
  158. {
  159.   PutMemList(&OutputMemList);
  160. }
  161. #endif  /*  AMIGA   */
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.     ProcessOutput() gets called, if a list of lines to be printed is setup
  168.     with lprint or "by hand". (see OutRound)
  169.  
  170.     Inputs: list -  list of lines to be printed; this can be NULL, in which
  171.             case OutputList is assumed
  172. */
  173. void ProcessOutput(void)
  174.  
  175. #ifdef AMIGA
  176. { struct MinNode *line;
  177.  
  178.  
  179.   /*
  180.       Output to a file is rather simple...
  181.   */
  182.   if (deviceOutput == DEVICE_FileAscii  ||  deviceOutput == DEVICE_FileTeX)
  183.   { FILE *fh;
  184.  
  185.     if ((fh = fopen(filenameOutput, "w"))  ==  NULL)
  186.     { ShowError((char *) MSG_NO_WRITE_FILE,
  187.         filenameOutput, IoErr());
  188.       OutputReturnCode = RETURN_ERROR;
  189.       return;
  190.     }
  191.  
  192.     if (deviceOutput == DEVICE_FileAscii  &&
  193.     (fputs(TrnName, fh) < 0  ||  fputs("\n\n", fh) < 0  ||
  194.      fputs(titleOutput, fh) < 0  ||  fputs("\n\n", fh) < 0  ||
  195.      fputs(prtheadOutput, fh) < 0  ||  fputs("\n\n", fh) < 0))
  196.     { OutputReturnCode = RETURN_ERROR;
  197.     }
  198.     else
  199.     { for(line = OutputList.mlh_Head;  line->mln_Succ != NULL;
  200.       line = line->mln_Succ)
  201.       { if (fputs((char *) (line+1), fh) < 0  ||
  202.         fputc('\n', fh) == EOF)
  203.     { OutputReturnCode = RETURN_ERROR;
  204.       break;
  205.     }
  206.       }
  207.     }
  208.     fclose(fh);
  209.   }
  210.  
  211.   /*
  212.       Output to the printer is a little bit more complicated, because it
  213.       is page oriented. The largest problem is to get the paper length.
  214.       Sigh! Why are the new preferences that complicated?
  215.   */
  216.   else if (deviceOutput != DEVICE_Screen)
  217.   { int paperlen, pagenr, lines;
  218.     FILE *fh;
  219.     int j, writeerr;
  220.  
  221.     /*
  222.     Get the paperlength
  223.     */
  224.     struct Preferences Prefs;
  225.  
  226.     GetPrefs(&Prefs, sizeof(Prefs));
  227.     paperlen = Prefs.PaperLength;
  228. #ifdef V39_INCLUDES
  229.     { struct IFFHandle *iffhandle;
  230.       struct StoredProperty *sp;
  231.       int ifferr;
  232.       extern struct Library *IFFParseBase;
  233.       extern struct Library *SysBase;
  234.  
  235.       if(IFFParseBase != NULL  &&  SysBase->LibNode.lib_Version >= 37)
  236.       { if ((iffhandle = AllocIFF())  !=  NULL)
  237.     { if ((iffhandle->iff_Stream = Open((STRPTR) "ENV:SYS/Printer.prefs",
  238.                         MODE_OLDFILE))
  239.                      !=  NULL)
  240.       { InitIFFasDOS(iffhandle);
  241.         if (OpenIFF(iffhandle, IFFF_READ)  ==  0)
  242.         { if (PropChunk(iffhandle, ID_PREF, ID_PTXT)  ==  0)
  243.           { for(;;)
  244.         { ifferr = ParseIFF(iffhandle, IFFPARSE_STEP);
  245.           if (ifferr  !=  0)
  246.           { if (ifferr  ==  IFFERR_EOC)
  247.             { continue;
  248.             }
  249.             else
  250.             { break;
  251.             }
  252.           }
  253.  
  254.           if ((sp = FindProp(iffhandle, ID_PREF, ID_PTXT))
  255.               !=  NULL)
  256.           { paperlen = ((struct PrinterTxtPrefs *)
  257.                    (sp->sp_Data))->pt_PaperLength;
  258.             break;
  259.           }
  260.         }
  261.           }
  262.           CloseIFF(iffhandle);
  263.         }
  264.         Close(iffhandle->iff_Stream);
  265.       }
  266.       FreeIFF(iffhandle);
  267.     }
  268.       }
  269.     }
  270. #endif
  271.  
  272.  
  273.     if ((fh = fopen("prt:", "w"))  ==  NULL)
  274.     { ShowError((char *) MSG_NO_PRINTER);
  275.       OutputReturnCode = RETURN_ERROR;
  276.       return;
  277.     }
  278.     writeerr = TRUE;
  279.  
  280.     /*
  281.     Put application into sleep mode.
  282.     */
  283.     set(App, MUIA_Application_Sleep, TRUE);
  284.  
  285.     /*
  286.     Put printer into draft or LQ mode
  287.     */
  288.     if (fprintf(fh, "%s",
  289.         (deviceOutput == DEVICE_PrinterDraft)  ?
  290.             "\2331\"z" : "\2332\"z")    <  0)
  291.     { goto WriteError;
  292.     }
  293.  
  294.  
  295.     line = OutputList.mlh_Head;
  296.     pagenr = 0;
  297.     while (line->mln_Succ != NULL)
  298.     {
  299.       if (fprintf(fh, "%s\n\n", TrnName)  <  0                  ||
  300.       fprintf(fh, "%-65s  %7s %d\n\n", titleOutput,
  301.           MSG_PAGENR, ++pagenr)  <  0   ||
  302.       fprintf(fh, "%s\n\n", prtheadOutput)  <  0)
  303.       { goto WriteError;
  304.       }
  305.       lines = 7 + prtlinesOutput;
  306.  
  307.       while (lines+prtlinesOutput < paperlen  &&  line->mln_Succ != NULL)
  308.       { lines += prtlinesOutput;
  309.     for (j = 0;  j < prtlinesOutput;  j++)
  310.     { if (fprintf(fh, "%s\n", line+1)  <  0)
  311.       { goto WriteError;
  312.       }
  313.       line = line->mln_Succ;
  314.     }
  315.       }
  316.  
  317.       while (lines++ < paperlen)
  318.       { if (fprintf(fh, "\n")  <  0)
  319.     { goto WriteError;
  320.     }
  321.       }
  322.  
  323.       if (fprintf(fh, "\n%s\n", PVERSION)  <  0     ||
  324.       fprintf(fh, "\f")  <  0)
  325.       { goto WriteError;
  326.       }
  327.     }
  328.     writeerr = FALSE;
  329.  
  330. WriteError:
  331.     fclose(fh);
  332.     if (writeerr)
  333.     { ShowError((char *) MSG_PrinterError);
  334.     }
  335.  
  336.     /*
  337.     Awake application
  338.     */
  339.     set(App, MUIA_Application_Sleep, FALSE);
  340.   }
  341.  
  342.   /*
  343.       At last output to the screen, which isn't very complicated too.
  344.       Thanks MUI.
  345.   */
  346.   else
  347.   { ULONG open, signal;
  348.     APTR OutWnd;        /*  output window                       */
  349.     APTR OutWnd_Head;   /*  page head gadget                    */
  350.     APTR OutWnd_LV;     /*  output text gadget                  */
  351.  
  352.     OutWnd = WindowObject,
  353.         MUIA_Window_ID, MAKE_ID('O','U','T','\0'),
  354.         MUIA_Window_Width, MUIV_Window_Width_MinMax(70),
  355.         MUIA_Window_Height, MUIV_Window_Height_MinMax(30),
  356.         MUIA_Window_Title, titleOutput,
  357.         WindowContents, VGroup,
  358.             Child, OutWnd_Head = TextObject,
  359.             MUIA_Text_Contents, scrheadOutput,
  360.             ReadListFrame,
  361.             MUIA_Font, MUIV_Font_Fixed,
  362.             End,
  363.             Child, OutWnd_LV = ListviewObject,
  364.             MUIA_Listview_List, FloattextObject,
  365.                 MUIA_Floattext_Text, OutputLine,
  366.                 ReadListFrame,
  367.                 MUIA_Font, MUIV_Font_Fixed,
  368.             End,
  369.             End,
  370.         End,
  371.         End;
  372.  
  373.     if (!OutWnd)
  374.     { return;
  375.     }
  376.  
  377.  
  378.     /*
  379.     Add the new window as a member to the application.
  380.     Setting up the notification events for the output window:
  381.     CloseWindow gadget only
  382.     */
  383. #define ID_OutWnd_Cancel 100
  384.     DoMethod(App, OM_ADDMEMBER, OutWnd);
  385.     DoMethod(OutWnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
  386.          App, 2, MUIM_Application_ReturnID, ID_OutWnd_Cancel);
  387.  
  388.     /*
  389.     Open the window
  390.     */
  391.     set(OutWnd, MUIA_Window_Open, TRUE);
  392.     get(OutWnd, MUIA_Window_Open, &open);
  393.     if (!open)
  394.     { MUIError((char *) ERRMSG_CANNOT_OPEN_WINDOW);
  395.       DoMethod(App, OM_REMMEMBER, OutWnd);
  396.       MUI_DisposeObject(OutWnd);
  397.       return;
  398.     }
  399.  
  400.     /*
  401.     Close the main window while the output window is open
  402.     */
  403.     set(MainWnd, MUIA_Window_Open, FALSE);
  404.  
  405.     /*
  406.     Wait for user actions
  407.     */
  408.     for(;;)
  409.     { switch(DoMethod(App, MUIM_Application_Input, &signal))
  410.       { case MUIV_Application_ReturnID_Quit:
  411.       if (TestSaved())
  412.       { exit(0);
  413.       }
  414.       break;
  415.     case ID_OutWnd_Cancel:
  416.       set(OutWnd, MUIA_Window_Open, FALSE);
  417.       DoMethod(App, OM_REMMEMBER, OutWnd);
  418.       MUI_DisposeObject(OutWnd);
  419.       return;
  420.       }
  421.  
  422.       if (signal)
  423.       { Wait(signal);
  424.       }
  425.     }
  426.   }
  427. }
  428. #endif  /*  AMIGA   */
  429.  
  430.  
  431.  
  432.  
  433. /*
  434.     AskForBirthday() should bring up a requester and ask for the birthday
  435.     of a player whose birthday field isn't set or valid.
  436.  
  437.     Inputs: plr - the player who is asked for
  438.  
  439.     Results: 1 = Assume that player is 20 or younger
  440.          2 = Assume that player is between 21 and 25 years old
  441.          3 = Assume that player is 26 or older
  442.          4 = The player wants to modify the players data
  443.          5 = Skip this player (will not be present in the DWZ report)
  444.          0 = Cancel
  445. */
  446. int AskForBirthday(struct Player *plr)
  447.  
  448. #ifdef AMIGA
  449. {
  450.   return(MUI_Request(App, MainWnd, 0,
  451.              (char *) MSG_ATTENTION,
  452.              (char *) MSG_NO_BIRTHDAY_GADGETS,
  453.              (char *) MSG_NO_BIRTHDAY,
  454.              plr->Name));
  455. }
  456. #endif  /*  AMIGA   */
  457.